#pragma once

#include <pthread.h>

namespace LockModule
{
    class Mutex
    {
    private:
        pthread_mutex_t _mutex;

    public:
        Mutex(const Mutex &mt) = delete;
        const Mutex &operator=(const Mutex &mt) = delete;

        Mutex()
        {
            pthread_mutex_init(&_mutex, NULL);
        }
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }

        void lock()
        {
            // 加锁失败后阻塞式等待
            int n = pthread_mutex_lock(&_mutex);
        }

        void unlock()
        {
            int n = pthread_mutex_unlock(&_mutex);
        }

        // 获取Mutex原始锁指针
        pthread_mutex_t *GetMutexOriginal()
        {
            return &_mutex;
        }
    };

    class LockGuard
    {
    private:
        Mutex& _mutex;
    public:
        LockGuard(Mutex& mutex)
        :_mutex(mutex)
        {
            _mutex.lock();
        }
        ~LockGuard()
        {
           _mutex.unlock();
        }
    };
}
